
var $table = $('#addTo-index-goods');

$(function () {

    initUngroupedTable(1);
});



// 初始化
function initUngroupedTable(selectedGroupType) {
    // 初始化bootstrap-table
    var url=urlcore + "/api/questionnaire/selectAll?type=1";
    var id = location.href.split("?id=")[1];
    var params = {
        id:id
    };
    initGroupTable(url,params);

}


//添加
function add(){
	var selection = $table.bootstrapTable('getSelections');

    if (selection.length == 0) {
        alert("请先选择问卷");
        return;
    }else if(selection.length >1){
        alert("只能选择一条");
        return;
    }
    var goodsId = location.href.split("?id=")[1];
    var goodsQuerstion = [];
    for (var item of selection) {
        goodsQuerstion.push({
            questionnaireId: item.id,
            goodsId:goodsId
        });
    }
    console.log(goodsQuerstion[0].questionnaireId);
//headers: {"Content-Type": "application/json;charset=UTF-8"},
    //contentType: "application/json;charset=UTF-8",


    $.ajax({
        type: "get",
        url: urlcore + "/api/goodsQuerstion/goodsQuesAdd?questionnaireId="+goodsQuerstion[0].questionnaireId+"&goodsId="+goodsId,
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: {},
        success: function (data) {
            alert("添加成功");
            window.location.href = "exp_question_list.html";
        },
        error: function () {
            alert("失败");
        }
    });



}


//初始化表格，有管理已有商品和添加分组商品两类表格
function initGroupTable(url,params){
    $table.bootstrapTable({
        columns: [{
            field: "state",
            checkbox: true,
            title: "ID"
        }, {
            field: "id",
            title: "ID"
        }, {
            field: "title",
            title: "标题"
        }],
        method: "get",
        url:url ,
        dataType: "json",
        queryParams: params,
        //返回数据预处理
        responseHandler: function (cbdata) {
            console.log(cbdata);
            var res = cbdata.data.records;
            return res;
        },
        toolbar:'#addGroupToolbar',
        pagination: true,//启用分页条
        sidePagination: "client",
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,
        sortable: true,//是否启用排序
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,
        // searchText:"",//初始搜索文本

    });
    
    
}